//
//  MainJokesViewController.m
//  project
//
//  Created by 张振宇 on 15/11/4.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "MainJokesViewController.h"
#import "PKRequestManager.h"
@interface MainJokesViewController ()
@property (nonatomic ,strong) NSMutableArray *arr;
@end

@implementation MainJokesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestData];
    self.navigationItem.title = @"段子";
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuseIdentifier"];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)requestData
{
    [PKRequestManager requestWithTypeP:GET urlString:@"http://s.budejie.com/topic/list/zuixin/29/baisishequ-iphone-3.7/0-20.json?appname=baisishequ&asid=061A9E01-D72B-4EC3-82E6-F2FA78CEACB6&client=iphone&device=ios%20device&from=ios&jbk=0&mac=&market=&openudid=ef3d2313a461111c5408d1882b5e910c86767678&udid=&uid=16396772&ver=3.7" parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        _arr = [NSMutableArray arrayWithCapacity:0];
        for (NSDictionary *dict in dic[@"list"]) {
            
            [_arr addObject:dict[@"text"]];
        }
        [self.tableView reloadData];
    } error:^(NSError *error) {
        
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return _arr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16] forKey:NSFontAttributeName];
    CGRect bounds = [_arr[indexPath.row] boundingRectWithSize:CGSizeMake(kWidth - 60, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return bounds.size.height + 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseIdentifier" forIndexPath:indexPath];
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:16] forKey:NSFontAttributeName];
    CGRect bounds = [_arr[indexPath.row] boundingRectWithSize:CGSizeMake(kWidth - 60, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kWidth, bounds.size.height + 60)];
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kWidth, 20)];
    [img setImage:[UIImage imageNamed:@"duanzi1"]];
    UIImageView *img1 = [[UIImageView alloc]initWithFrame:CGRectMake(-2, bounds.size.height + 40, kWidth, 20)];
    [img1 setImage:[UIImage imageNamed:@"duanzi2"]];
    [backView addSubview:img1];
    [backView addSubview:img];
    [cell setBackgroundView:backView];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = _arr[indexPath.row];
    cell.textLabel.frame = CGRectMake(20, 20, kWidth - 40, bounds.size.height + 40);
    [cell.textLabel setBackgroundColor:[UIColor clearColor]];
    [cell.textLabel setFont:[UIFont systemFontOfSize:16]];
    cell.textLabel.numberOfLines = 0;
    // Configure the cell...
    
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
